#!/bin/bash
#--2021-11-22
#--linjinting
##

OS_RELEASE_FILE="/etc/os-release"
OS_RELEASE_FILE_6="/etc/system-release"
#NEOKYLIN_RELEASE_FILE="/etc/neokylin-release"
LINX_RELEASE_FILE="/etc/linx-release"

OTHER_OS_ARRAY=("neokylin", "linx")
OS_CODE=0
SYSTEMCTL_TYPE=0

function os_release() {
    system=`uname`
    nodename=`uname -n`
    machine=`uname -m`
    kernel=`uname -r`

    if [ 'Linux' == ${system} ];then
        if [ -f ${OS_RELEASE_FILE} ];then
            source ${OS_RELEASE_FILE}
        elif [ -f ${OS_RELEASE_FILE_6} ];then
            NAME=$(cat ${OS_RELEASE_FILE_6}|sed -r 's/(\w+) (release|version).*/\1/')
            VERSION_ID=$(cat ${OS_RELEASE_FILE_6}|sed -r 's/.* ([0-9]+.?[0-9]+)..*/\1/')
        elif [ -f ${LINX_RELEASE_FILE} ];then
            NAME="linx"
            VERSION_ID=$(cat ${LINX_RELEASE_FILE}|awk -F "." '{print $1}')
        fi

        if  command -v getconf > /dev/null;then
            LONG_BIT=$(getconf LONG_BIT)
        fi
    else
        echo -e "no support the system ${system}\n"
    fi
}

function format_info() {
    echo "***********************************"
    printf "操作系统类型: %s \n主机名称: %s \n主机架构: %s \n内核版本: %s \n发行版本名称: %s \n发行版本号: %s \n系统位数: %s \n自定义代码: %s\n" \
    "${system}" "${nodename}" "${machine}" "${kernel}" "${NAME}" "${VERSION_ID}" "${LONG_BIT}" "${OS_CODE}"
    echo "***********************************"
}

function set_code() {
    if [ "${NAME}" == "Red Hat Enterprise Linux Server" ] && version_ge "${VERSION_ID}" "7.0" ;then
        OS_CODE=100101
    elif [ "${NAME}" == "Red Hat Enterprise Linux Server" ] && version_lt "${VERSION_ID}" "7.0" ; then
        OS_CODE=100102
    elif [ "${NAME}" == "Centos Linux" ] &&  version_ge "${VERSION_ID}" "7.0" ]; then
        OS_CODE=100111
    elif [ "${NAME}" == "Centos Linux" ] &&  version_lt "${VERSION_ID}" "7.0" ; then
        OS_CODE=100112
    elif [ "${NAME}" == "NeoKylin Linux Advanced Server" ]; then
        OS_CODE=100142            # server6.5
    elif [ "${NAME}" == "NeoKylin Linux Desktop" ] && [[ "${LONG_BIT}" == "64" ]]; then
        OS_CODE=100141
    elif [ "${NAME}" == "NeoKylin Linux Desktop" ] && [[ "${LONG_BIT}" == "32" ]]; then
        OS_CODE=101141
    elif [ "${NAME}" == "UniKylin Linux" ] &&  version_ge "${VERSION_ID}" "3" ; then
        OS_CODE=100151
    elif [ "${NAME}" == "Debian GUN/Linux" ] &&  version_ge "${VERSION_ID}" "9.0" ; then
        OS_CODE=100201
    elif [ "${NAME}" == "Debian GUN/Linux" ] &&  version_lt "${VERSION_ID}" "9.0" ; then
        OS_CODE=100202
    elif [ "${NAME}" == "Ubuntu" ] && version_ge "${VERSION_ID}" "16.04" ; then
        OS_CODE=100211
    elif [ "${NAME}" == "Ubuntu" ] &&  version_lt "${VERSION_ID}" "16.04" ; then
        OS_CODE=100212
    elif [ "${NAME}" == "Linx GNU/Linux" ]; then
        OS_CODE=100221            #80
    elif [ "${NAME}" == "linx" ]; then
        OS_CODE=100222            #60
    elif [ "${NAME}" == "kylin" ]; then
        OS_CODE=100231            # 银河麒麟
    elif [ "${NAME}" == "NewStart CGS Linux" ] && version_ge "${VERSION_ID}" "5.5" ; then
        OS_CODE=100161            # 中兴新支点
    fi
}

function check_systemctl_type() {
        # redhat>6.9 centos>6.9 NeoKylin-Desktop Debian>8 Ubuntu>14.10 Linx80 UniKylin>2
        systemd_os_array=(0 100101 100111 100141 101141 100151 100201 100211 100221 100213 100161)
        # redhat<7.0  centos<7.0 NeoKylin-SERVER-6.5  Linx60
        service_os_array=(100102 100112 100142 100222)
        # isContains "${systemd_os_array[*]}" "${OS_CODE}"
        if [ "$(command -v systemctl)" ];then
            SYSTEMCTL_TYPE=1
        elif [ "$(command -v chkconfig)" ]; then
            SYSTEMCTL_TYPE=2
        # Ubuntu<16.40
        elif [ ${OS_CODE} == 100212 ]; then
            SYSTEMCTL_TYPE=3
    	else
    		echo -e "\033[31m unrecognized this system for code:${OS_CODE} \033[0m\n"
    	fi
}
source  ../unit/unit.sh
os_release
set_code